import Head from 'next/head'
import { dataService } from '../../services'
import { restoreId } from '../../utils'
import styles from '../../styles/detail.module.css'

export default function Detail(props: IDetailModel) {
  return (
    <div className="main-container">
      <Head>
        <title>{props.title} - {process.env.siteName}</title>
      </Head>
      <h1 className={styles.title}>{props.title}</h1>
      <div className={styles.time}>发布时间：{props.releaseTime}</div>
      <div className={styles.content} dangerouslySetInnerHTML={{ __html: props.content }}></div>
    </div>
  )
}

export async function getServerSideProps(props: any) {
  let [id] = props.query.all
  const $ = await dataService.getDetailDoc(restoreId(id))
  let model = {} as IDetailModel
  model.title = $('.title_all h1 font').text()
  let $contentWrap = $('.co_content8 ul')

  let matchRes = $contentWrap.text().match(/发布时间：([0-9]{4}\-[0-9]{2}\-[0-9]{2})/)
  if (matchRes && matchRes.length >= 2) {
    model.releaseTime = matchRes[1]
  }
  let $content = $contentWrap.find('#Zoom > span').remove('script')
  $content.find('center').remove()
  $content.find('> font').remove()
  $content.find('hr').remove()
  $content.find('script').remove()
  model.content = $content.html()!
  return { props: model }
}

interface IDetailModel {
  title: string
  releaseTime: string
  content: string
}